package com.product;

import java.awt.Dialog;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import com.dao.UserDaoImp;
import com.order.OrderDao;
import com.vo.Order;
import com.vo.OrderDetail;
import com.vo.ShopCart;
import com.vo.Users;

public class OrderServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 放的是 购物车中需要结算的商品的ID
		int spId = 0;
		HttpSession session = request.getSession();
		// 得到购物车的商品
		List<ShopCart> shopCarts = (List) session.getAttribute("shopcartlist");

		// 得到购买商品的用户 用于进行筛选用户
		String oUser = (String) session.getAttribute("Uname");

		// Users DAO
		UserDaoImp dao = new UserDaoImp();
		// Orser DAO
		OrderDao orderDao = new OrderDao();
		// 用来接收筛选后的用户信息
		Users users = new Users();

		List<Users> usersList = new ArrayList<Users>();
		try {
			usersList = dao.getAllData();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 筛选
		for (int a = 0; a < usersList.size(); a++) {
			if (oUser.equals(usersList.get(a).getEu_user_name())) {
				users = usersList.get(a);
			}
		}
		List<OrderDetail> oDList = new ArrayList<OrderDetail>();
		Order order = new Order();
		if (request.getParameterValues("ck") != null) {
			String[] spid = request.getParameterValues("ck");
			for (int b = 0; b < spid.length; b++) {

				spId = Integer.parseInt(spid[b]);

				for (int a = 0; a < shopCarts.size(); a++) {

					if (spId == shopCarts.get(a).getSp_id()) {
						int id = shopCarts.get(a).getSp_id();
						OrderDetail detail = new OrderDetail();
						detail.setEp_id(shopCarts.get(a).getSp_id());
						detail.setEod_quantity(shopCarts.get(a).getSp_num());
						detail.setEod_cost(shopCarts.get(a).getSp_price());
						oDList.add(detail);

						
					}

				}
			}
			// 为Order中添加值
			
			order.setEo_user_id(users.getEu_user_id());
			order.setEo_user_name(users.getEu_user_name());
			order.setEo_user_address(users.getEu_address());
			order.setEo_tel(users.getEu_mobile());
			order.setEo_status(1);
			order.setEo_type(Integer.parseInt(request.getParameter("eo_type")));
			float cost = 0;
			for (int a = 0; a < oDList.size(); a++) {
				cost += oDList.get(a).getEod_cost()
						* oDList.get(a).getEod_quantity();
			}
			order.setEo_cost(cost);
		
			boolean flag = false;
			try {

				flag = orderDao.insert(order, oDList);
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (flag) {
				for(int a=0;a<oDList.size();a++){
					for(int b=0;b<shopCarts.size();b++){
						int id = shopCarts.get(b).getSp_id();
						if(oDList.get(a).getEp_id() == id){
							shopCarts.remove(shopCarts.get(b));
							
						}
					}
				}
				
				response.sendRedirect("shopping-result.jsp");
			} 
		
		}else{
			
			response.sendRedirect("shopping-result-error.jsp");
		}

		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
